package service

import (
	"fmt"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/model"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/app/admin/internal/vo"
	"github.com/vueadmin/utils/conv"
	"errors"
)

type roleService struct {
	svcCtx *svc.ServiceContext
}

func NewRoleService(svcCtx *svc.ServiceContext) *roleService {
	return &roleService{
		svcCtx: svcCtx,
	}
}

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func (p roleService) GetPageList(req *dto.RolePageReq) ([]*vo.RoleList, int64, error) {
	var (
		entity model.Role
		list  []*vo.RoleList
		count int64
	)
	query := p.svcCtx.Db.Model(&entity)
	if !conv.IsEmpty(req.RoleId) {
		query = query.Where("role_id = ?", req.RoleId)
	}
	if !conv.IsEmpty(req.Name) {
		query = query.Where("name = ?", req.Name)
	}
	if !conv.IsEmpty(req.Status) {
		query = query.Where("status = ?", req.Status)
	}
	orderBy := "role_id desc"
	if !conv.IsEmpty(req.Sort) && !conv.IsEmpty(req.Order) {
		orderBy = fmt.Sprintf("%s %s", req.Sort, req.Order)
	}
	query.Count(&count)
	if err := query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit).Order(orderBy).Find(&list).Error; err != nil {
		return nil, 0, err
	}
	return list, count, nil
}

/**
 * @description(修改排序开关)
 * @buildcode(true)
*/
func (p roleService) UpdateExt(req map[string]interface{}) error {
	entity := model.Role{}
	if err := p.svcCtx.Db.Model(&entity).Where("role_id",conv.Int(req["role_id"])).Updates(req).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(添加)
 * @buildcode(false)
*/
func (p roleService) Add(req *dto.RoleAddReq) (int, error) {
	entity := model.Role{}
	entity.Name = req.Name
	entity.Status = req.Status
	entity.Description = req.Description
	entity.Access = req.Access

	if err := p.svcCtx.Db.Create(&entity).Error; err != nil {
		return 0, err
	}
	return entity.RoleId, nil
}

/**
 * @description(修改)
 * @buildcode(false)
*/
func (p roleService) Update(req *dto.RoleUpdateReq) error {
	entity := model.Role{
		RoleId: req.RoleId,
	}
	query := p.svcCtx.Db
	if err := query.Limit(1).First(&entity).Error; err != nil {
		return err
	}
	entity.Name = req.Name
	entity.Status = req.Status
	entity.Description = req.Description
	entity.Access = req.Access

	field := conv.StringToSlice("name,status,description,access", ",")

	if err := query.Select(field).Updates(&entity).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(获取修改详情)
 * @buildcode(true)
*/
func (p roleService) GetUpdateInfo(req *dto.RoleGetUpdateInfoReq) (*model.Role, error) {
	entity := model.Role{}
	if err := p.svcCtx.Db.Where("role_id = ?",req.RoleId).Limit(1).First(&entity).Error; err != nil {
		return nil, err
	}
	return &entity, nil
}

/**
 * @description(删除)
 * @buildcode(false)
*/
func (p roleService) Delete(req *dto.RoleDeleteReq) error {
	if len(req.RoleId) > 1 {
		return errors.New("禁止批量删除")
	}
	if req.RoleId[0] == 1 {
		return errors.New("超级管理员禁止删除")
	}
	query := p.svcCtx.Db
	query = query.Where("role_id IN ?", req.RoleId)
	if err := query.Delete(&model.Role{}).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func (p roleService) Detail(req *dto.RoleDetailReq) (*vo.RoleDetail, error) {
	var info vo.RoleDetail
	query := p.svcCtx.Db.Model(&model.Role{})
	if err := query.Where("role_id = ?",req.RoleId).Limit(1).First(&info).Error; err != nil {
		return nil, err
	}
	return &info, nil
}

